<?php
header('Content-Type: application/json');
Header('Access-Control-Allow-Methods:GET,POST,PUT,DELETE');
header("Access-Control-Allow-Headers: Content-Type");
header('Access-Control-Allow-Origin:*');   //跨域

require './mysql_init.php';



//判断该用户是否有此地址
function limit_is_user_address($pdo,$uid,$address_id){
    try {
        // 准备查询语句
        $query = "SELECT count(*) FROM shipping_address WHERE user_uid=? and address_id=?";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $uid);
        $statement->bindParam(2, $address_id);
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->fetchAll()[0]['count(*)'];      //把查询结构赋值给$count
        return $result;
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }
}
//修改地址
function change_address($pdo,$uid,$address_id,$province,$detailed_address,$phone_number){
    try {
        // 准备修改语句
        $query = "UPDATE shipping_address SET province=?, detailed_address=?, phone_number=? WHERE address_id=? and user_uid=?";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $province);
        $statement->bindParam(2, $detailed_address);
        $statement->bindParam(3, $phone_number);
        $statement->bindParam(4, $address_id);
        $statement->bindParam(5, $uid);
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->rowCount();      //把查询结构赋值给$count
        return $result;
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }
}
//判断是否是商家
function is_seller($pdo,$uid){
    try {
        // 准备查询语句
        $query = "SELECT count(*) FROM sellers WHERE user_uid = ?";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $uid);
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->fetchAll()[0]['count(*)'];      //把查询结构赋值给$count
        return $result;
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }
}
//获取商家id
function get_sellerid($pdo,$uid){
    try {
        // 准备查询语句
        $query = "SELECT seller_id FROM sellers WHERE user_uid = ?";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $uid);
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->fetchAll()[0]['seller_id'];      //把查询结构赋值给$count
        return $result;
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }
}
//判断该用户是否有此商品
function limit_is_user_goods($pdo,$goods_id,$seller_id){
    try {
        // 准备查询语句
        $query = "SELECT count(*) FROM goods WHERE goods_id=? and seller_id=?";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $goods_id);
        $statement->bindParam(2, $seller_id);
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->fetchAll()[0]['count(*)'];      //把查询结构赋值给$count
        return $result;
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }

}


//更改商品信息
function change_goods_news($pdo,$goods_id,$judge_len){
    try {
        // 准备修改语句
        $query = "UPDATE goods SET name=?, price=?,count=? WHERE goods_id=?;";
        $statement = $pdo->prepare($query);
        $statement->bindParam(4, $goods_id);
        // 执行查询
        $statement->execute($judge_len);
        // 获取结果
        $result = $statement->rowCount();      //把查询结构赋值给$count
        return $result;
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }

}

//获取默认地址的id
function get_default_address_id($pdo,$uid){
    try {
        // 准备查询语句
        $query = "SELECT address_id FROM shipping_address WHERE user_uid=? and is_forbidden=0 and is_default_address=1";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $uid);
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->fetchAll()[0]['address_id'];      //把查询结构赋值给$count


        $query2 = "SELECT count(*) FROM shipping_address WHERE user_uid=? and is_forbidden=0 and is_default_address=1";
        $statement2 = $pdo->prepare($query2);
        $statement2->bindParam(1, $uid);
        // 执行查询
        $statement2->execute();
        $result2 = $statement2->fetchAll()[0]['count(*)'];      //把查询结构赋值给$count

        if($result2==1){
            return $result;
        }else{
            return 0;
        }
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }
}
//获取订单中的信息
function get_orders_news($pdo,$uid,$order_id){
    try {
        // 准备查询语句
        $query = "SELECT COUNT(*) FROM orders WHERE order_id =? AND user_uid =? and user_is_payment=0";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $order_id);
        $statement->bindParam(2, $uid);
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->fetchAll()[0]['COUNT(*)'];      //把查询结构赋值给$count
        if($result==1){
            // 准备查询语句
            $query2 = "SELECT g.name,o.order_price,o.count,sa.province ,sa.detailed_address ,sa.phone_number ,o.create_time  FROM goods g,orders o,shipping_address sa WHERE o.order_id =? AND g.goods_id =o.goods_id AND o.user_uid =sa.user_uid AND o.user_is_payment =0;";
            $statement2 = $pdo->prepare($query2);
            $statement2->bindParam(1, $order_id);
            // 执行查询
            $statement2->execute();
            // 获取结果
            $result2 = $statement2->fetchAll()[0];      //把查询结构赋值给$count
            return $result2;
        }else{
            return 0;
        }
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }
}
//获取购物车列表
function get_shopping_cat_list($pdo,$uid){
    try {
        // 准备查询语句
        $query = "SELECT sc.goods_cat_id, g.name ,g.price , sc.count,g.cover_img_path,g.goods_id FROM shopping_cat sc ,goods g  WHERE sc.uid =? AND sc.goods_id =g.goods_id AND sc.is_pay=0";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $uid);
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->fetchAll();      //把查询结构赋值给$count
        return $result;
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }
}

//获取订单列表
function get_orders_list1($pdo,$uid){
    try {
        // 准备查询语句
        $query = "SELECT o.order_id, g.goods_id,g.name ,o.order_price, o.count, o.create_time, o.user_is_payment, o.user_is_cancel, o.is_send_out_goods, o.shller_is_cancle FROM orders o ,goods g  WHERE o.user_uid=? AND  g.goods_id =o.goods_id ";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $uid);
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->fetchAll();      //把查询结构赋值给$count
        return $result;
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }
}
//取消订单
function get_orders_list($pdo,$order_id,$uid){
    try {
        // 准备查询语句
        $query = "UPDATE orders SET user_is_cancel=1 WHERE order_id=? AND user_uid =?";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $order_id);
        $statement->bindParam(2, $uid);
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->rowCount();      //把查询结构赋值给$count
        return $result;
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }
}
//获取订单列表,已支付而未发货，未取消的订单
function get_orders_list11($pdo,$uid){
    try {
        // 准备查询语句
        $query="SELECT o.order_id, g.goods_id,g.name ,o.order_price, o.count,g.cover_img_path, o.create_time, o.user_is_payment, o.user_is_cancel, o.is_send_out_goods, o.shller_is_cancle 
        FROM orders o ,goods g  
        WHERE o.user_uid=? AND  g.goods_id =o.goods_id AND user_is_payment = 1 AND o.user_is_cancel =0 AND o.shller_is_cancle =0 AND o.is_send_out_goods =0;";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $uid);
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->fetchAll();      //把查询结构赋值给$count
        return $result;
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }
}

//获取指定order_id的信息，
function get_order_info($pdo,$uid,$order_id){
    try {
        // 准备查询语句
        $query="SELECT o.order_id, g.goods_id,g.name ,o.order_price, o.count,g.cover_img_path, o.create_time, o.user_is_payment, o.user_is_cancel, o.is_send_out_goods, o.shller_is_cancle 
        FROM orders o ,goods g  
        WHERE o.user_uid=? AND o.order_id  =? AND  g.goods_id =o.goods_id AND o.user_is_payment = 1;";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $uid);
        $statement->bindParam(2, $order_id);
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->fetchAll()[0];      //把查询结构赋值给$count
        return $result;
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }
}
$uid=7;
$address_id=66;
$province='qq';
$detailed_address='ww';
$phone_number='125';
$goods_id=1;
$seller_id=9;
$order_id=14;

$judge_len=[];
// print_r(get_orders_news($pdo,$uid,$order_id));
$as=get_order_info($pdo,$uid,$order_id);
print_r($as);
var_dump(count($as));
// echo limit_is_user_address($pdo,$uid,$address_id);
// echo change_address($pdo,$uid,$address_id,$province,$detailed_address,$phone_number);

?>